package com.youdoneed.community.forum.service;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import com.github.pagehelper.PageHelper;
import com.youdoneed.base.application.AppException;
import com.youdoneed.base.application.ResultMessage;
import com.youdoneed.community.common.utils.StringUUIdUtil;
import com.youdoneed.community.forum.dao.ForummessageMapper;
import com.youdoneed.community.forum.dao.ForumreportMapper;
import com.youdoneed.community.forum.entity.Forummessage;
import com.youdoneed.community.forum.entity.Forumreport;
import com.youdoneed.user.feign.UserFeignClient;
import com.youdoneed.user.model.User;

/**
 * 举报模块服务接口
 *
 * @author lid
 * @date 2017.6.9
 */
@Service
public class ForumreportService {

    private final Logger logger = LoggerFactory.getLogger(ForumreportService.class);

    @Autowired
    ForumreportMapper forumreportMapper;

    @Autowired
    ForummessageMapper forummessageMapper;

    @Autowired
    UserFeignClient userFeignClient;

    public static final Integer reportNumberTwo = 2;

    /**
     * 保存举报数据
     * 将帖子的举报数+1
     *
     * @param forumreport
     * @return int
     * @author lid
     * @date 2017.6.9
     */
    @Transactional
    public int saveForumreport(Forumreport forumreport) {
        logger.info("saveForumreport");
        if (null == forumreport) {
            throw new AppException("forumreport参数为空!");
        }

        if (!StringUtils.hasText(forumreport.getForumMsgId())) {
            throw new AppException("forumMsgId参数为空!");
        }
        logger.info("ForumMsgId:" + forumreport.getForumMsgId());
        //根据id查找帖子信息
        Forummessage msg = forummessageMapper.selectByPrimaryKey(forumreport.getForumMsgId());
        if (null == msg) {
            //如果帖子为空则表示举报的是新闻
            //TODO  暂时不做处理
            return 0;
        }

        if (reportNumberTwo == msg.getReportCount()) {
            //逻辑删除帖子
            logger.info("举报次数达到3次，帖子逻辑删除，并删除分享帖！");
            forummessageMapper.updateById(msg.getForumMsgId());
            forummessageMapper.deleteShareMsgsByLogic(msg.getForumMsgId());
        }

        logger.info("msg.getForumMsgId()：" + msg.getForumMsgId());
        logger.info("msg.getReportCount()：" + msg.getReportCount());
        try {
            forumreport.setForumReportId(StringUUIdUtil.getUUID());
            forumreport.setCreateDate(new Date());
            forumreport.setForumHandle(ForumServciceStatic.forum_handle_no);
            int result = forumreportMapper.insert(forumreport);
            //帖子的举报数+1
            forummessageMapper.addReportNumberByPrimaryKey(msg.getForumMsgId());
            return result;
        } catch (Exception e) {
            logger.info("保存举报信息失败" + e);
            return 0;
        }
    }

    /**
     * 删除举报信息
     *
     * @param forumReportId
     * @return int
     * @author lid
     * @date 2017.6.9
     */
    @Transactional
    public int deleteForumreport(String forumReportId) {
        if (!StringUtils.hasText(forumReportId)) {
            logger.info("deleteForumreport!forumReportId参数为空!");
            throw new AppException("forumReportId参数为空!");
        }
        logger.info("deleteForumreport!forumReportId:" + forumReportId);

        try {
            return forumreportMapper.deleteByPrimaryKey(forumReportId);
        } catch (Exception e) {
            logger.info("删除举报信息失败" + e);
            return 0;
        }
    }

    /**
     * 通过id查找举报信息
     *
     * @param forumreportId
     * @return Forumreport
     * @author lid
     * @date 2017.6.9
     */
    @Transactional
    public Forumreport findByForumreportId(String forumreportId) {
        if (!StringUtils.hasText(forumreportId)) {
            logger.info("deleteForumreport!forumReportId参数为空!");
            throw new AppException("forumReportId参数为空!");
        }

        logger.info("findByForumreportId!forumreportId:" + forumreportId);

        try {
            return forumreportMapper.selectByPrimaryKey(forumreportId);
        } catch (Exception e) {
            logger.info("查询举报信息失败" + e);
            return null;
        }
    }

    /**
     * 通过msgId查找当前帖子的所有举报信息
     *
     * @param forumMsgId
     * @return List<Forumreport>
     * @author lid
     * @date 2017.6.9
     */
    @Transactional
    public List<Forumreport> findForumreportByMsgId(String forumMsgId) {
        if (!StringUtils.hasText(forumMsgId)) {
            logger.info("findForumreportByMsgId!forumMsgId参数为空!");
            throw new AppException("forumMsgId参数为空!");
        }
        try {
            List<Forumreport> listForumreport = forumreportMapper.findForumreportByMsgId(forumMsgId);
            for (int index = 0; index < listForumreport.size(); index++) {
                ResultMessage<User> user = userFeignClient.getUser(listForumreport.get(index).getReportUserId());
                listForumreport.get(index).setName(user.getData().getName());
                logger.info(listForumreport.get(index).getName() + "哼哼哈嘿恍恍惚惚");
            }
            logger.info("listForumreport.size():" + listForumreport.size());
            return listForumreport;
        } catch (Exception e) {
            logger.info("查询举报信息失败" + e);
            return null;
        }
    }

    /**
     * 根据被举报原因和创建时间查找被举报帖子，用于获取帖子总数
     *
     * @param forumReportReason
     * @param createDateS
     * @param createDateE
     * @return List<Forumreport>
     * @author lixs
     * @date 2017.6.13
     */
    @Transactional
    public List<Forumreport> getSelectReportForumMsg(String forumReportReason, String createDateS, String createDateE) {
        try {
            List<Forumreport> listForumreport = forumreportMapper.getSelectReportForumMsg(forumReportReason, createDateS, createDateE);
            return listForumreport;
        } catch (Exception e) {
            logger.info("获取被举报帖子失败" + e);
            return null;
        }

    }

    /**
     * 根据被举报原因和创建时间查找被举报帖子并分页
     *
     * @param forumReportReason
     * @param createDateS
     * @param createDateE
     * @param rows
     * @param currentPage
     * @author lixs
     * @date 2017.6.13
     */
    @Transactional
    public List<Forumreport> getSelectReportForumMsgByPage(String forumReportReason, String createDateS, String createDateE, Integer rows, Integer currentPage) {
        try {
            PageHelper.startPage(currentPage, rows);
            List<Forumreport> listForumreport = forumreportMapper.getSelectReportForumMsg(forumReportReason, createDateS, createDateE);
            return listForumreport;
        } catch (Exception e) {
            logger.info("获取被举报帖子失败" + e);
            return null;
        }
    }

    /**
     * 获取被举报帖子总数
     *
     * @return Integer
     */
    @Transactional
    public Integer getReportMsgsCount() {

        return forumreportMapper.getReportMsgsCount();
    }

    /**
     * 处理被举报帖子状态
     *
     * @param forumreportId
     * @param forumHandle
     * @author lixs
     * @Date 2017.8.8
     */
    @Transactional
    public int dealReportForumMessage(String forumMsgId, String forumHandle, String handlePerson) {
        if (!StringUtils.hasText(forumMsgId)) {
            throw new AppException("被举报帖子id不能为空");
        }
        if (!StringUtils.hasText(forumHandle)) {
            throw new AppException("被举报帖子处理状态不能为空");
        }

        Forummessage forummessage = forummessageMapper.selectByPrimaryKey(forumMsgId);
        if (null == forummessage) {
            throw new AppException("该帖子不存在");
        }
        List<Forumreport> forumreportlist = forumreportMapper.findForumreportByMsgId(forumMsgId);
        for (int index = 0; index < forumreportlist.size(); index++) {
            forumreportlist.get(index).setForumHandle(forumHandle);
            forumreportMapper.updateByPrimaryKeySelective(forumreportlist.get(index));

        }
        if (forumHandle.equals(ForumServciceStatic.forum_handle_yes_delete_yes)) {
            forummessage.setMsgDelete(1);
            forummessage.setHandlePerson(handlePerson);
            return forummessageMapper.updateByPrimaryKeySelective(forummessage);
        }
        return 0;

    }

}
